#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2025/2/2 22:14
# @Author  : Ramsey
# @Site    : zh
# @File    : items.py
# @Software: PyCharm
from copy import deepcopy
from collections.abc import MutableMapping

from spider_framework.items import Field, ItemMate


class Item(MutableMapping, metaclass=ItemMate):
    FIELDS: dict = dict()

    def __init__(self, *args, **kwargs):
        self._values: dict = {}
        if args:
            raise
        if kwargs:
            for key, value in kwargs.items():
                self[key] = value

    def __getitem__(self, item):
        return self._values.get(item)

    def __delitem__(self, key):
        del self._values[key]

    def __setitem__(self, key, value):
        if key in self.FIELDS:
            self._values[key] = value
        else:
            raise KeyError(f"{key} is not in {self.FIELDS}")

    def __setattr__(self, key, value):
        if not key.startswith("_"):
            raise AttributeError(f"please do not using `.` to assign")
        super().__setattr__(key, value)

    def __iter__(self):
        return iter(self._values)

    def __len__(self):
        return len(self._values)

    def copy(self):
        return deepcopy(self)

    def __repr__(self):
        return str(self._values)

    def to_dict(self):
        return dict(self)


if __name__ == "__main__":
    pass
